package cn.yunhe.action;

import cn.yunhe.pojo.House;
import cn.yunhe.service.HouseService;
import cn.yunhe.util.BaseAction;
import cn.yunhe.util.Pagination;
import com.opensymphony.xwork2.ModelDriven;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HouseAction extends BaseAction implements ModelDriven{
    private House house;
    private String target;
    private Map<String,Object>objectMap=new HashMap<>();

    private String htitle;
    private String hprice_range;
    private String hfloorage_range;
    private int street_district_did;
    private int street_sid;
    private int type_tid;
    private int currPageNo;
    private int maxRows;
    private int member_mid;
    private String operater;//标识符

    HouseService houseService=new HouseService();

    /**
     * 插入
     * @return
     */
    public String insertHouse(){
        //receiveParameter2House(house);
        boolean flag=houseService.insertHouse(house);
        if (flag){
            target="guanli";
            return target;
        }else {
            return ERROR;
        }
    }

    /**
     * 更新
     * @return
     */
    public String updateHouse(){
        receiveParameter2House(house);
        boolean flag=houseService.updateHouse(house);
        if (flag){
            target="gaunli";
            return target;
        }else {
            return ERROR;
        }
    }

    /**
     * 删除
     * @return
     */
    public String deleteHouse(){
        receiveParameter2House(house);
        boolean flag=houseService.deleteHouse(house.getHid());
        if (flag){
            target="guanli";
            return target;
        }else {
            return ERROR;
        }
    }


    /**
     * 查询
     * @return
     */
    public String houseList(){
        house.getStreet().getDistrict().setDid(street_district_did);
        house.getStreet().setSid(street_sid);
        house.getType().setTid(type_tid);
        house.getMember().setMid(member_mid);
        //核查接收的参数集合
        receiveParameter2House(house);

        int hprice2Left=-1;
        int hprice2Right=-1;
        int hfloorage2Left=-1;
        int hfloorage2Right=-1;
        if (hprice_range!=null){
            String[] hprice2Str=hprice_range.split("-");
            hprice2Left=Integer.parseInt(hprice2Str[0]);
            hprice2Right=Integer.parseInt(hprice2Str[1]);
        }
        if (hfloorage_range!=null){
            String[] hfloorage2Str=hfloorage_range.split("-");
            hfloorage2Left=Integer.parseInt(hfloorage2Str[0]);
            hfloorage2Right=Integer.parseInt(hfloorage2Str[1]);
        }

        Map<String,Object>map=new HashMap<>();
        map.put("htitle",house.getHtitle());
        map.put("hprice2Left",hprice2Left);
        map.put("hprice2Right",hprice2Right);
        map.put("hfloorage2Left",hfloorage2Left);
        map.put("hfloorage2Right",hfloorage2Right);
        map.put("street_district_did",street_district_did);
        map.put("street_sid",street_sid);
        map.put("type_tid",type_tid);
        map.put("member_mid",member_mid);
        System.out.println("111未添加分页参数"+map.values());


        //获取总记录数
        int totalRecords=houseService.houseListByMap2TotalRecords(map);
        //翻页功能
        Pagination<House> pagination=new Pagination<>();
        pagination.setTotalRecords(totalRecords);
        pagination.setMaxRows(maxRows);
        pagination.setCurrPageNo(currPageNo);
        checkPagination(pagination);


        map.put("startIndex",pagination.getStartIndex());
        map.put("maxRows",pagination.getMaxRows());
        System.out.println("2222添加分页参数"+map.values());
        List<House>houseList=new ArrayList<>();
        houseList=houseService.houseListByMap(map);

        objectMap.put("pagination",pagination);
        objectMap.put("houseList",houseList);
        target=SUCCESS;
        return target;
    }

    /**
     * 由hid定位到特定的房屋信息
     * @return
     */
    public String searchHouse(){
        System.out.println("searchHouse()======START");
        receiveParameter2House(house);
        System.out.println("searchHouse()======END");
        //通过hids条件刷选结果集只有1个
        List<House>houseList=houseService.houseList(house);
        for (House house:houseList){
            System.out.println("结果集的长度======"+houseList.size());
            System.out.println("结果集的第一个元素======"+houseList.get(0).getHtitle());
            System.out.println("结果集的标题======"+house.getHtitle());
        }
        getMap2Request().put("houseInfo",houseList.get(0));
        if ("details".equals(operater)){
            operater="details";
        }else if ("updateHouse".equals(operater)){
            operater="updateHouse";
        }
        return "operaterHouse";
    }

    @Override
    public House getModel() {
        if (house==null)
            house=new House();
        return house;
    }

    /**
     * 测试使用
     *
     */
    public void receiveParameter2House(House house){
        System.out.println("htitle======="+htitle);
        System.out.println("hprice_range======="+hprice_range);
        System.out.println("hfloorage_range======="+hfloorage_range);
        System.out.println("street_district_did======="+street_district_did);
        System.out.println("street_sid======="+street_sid);
        System.out.println("type_tid======="+type_tid);
        System.out.println("currPageNo======="+currPageNo);
        System.out.println("maxRows======="+maxRows);
        System.out.println("member_mid======="+member_mid);
        System.out.println("operater======="+operater);
        System.out.println("==================Start==================");
        System.out.println("getHid======="+house.getHid());
        System.out.println("getHtitle======="+house.getHtitle());
        System.out.println("getHprice======="+house.getHprice());
        System.out.println("getHfloorage======="+house.getHfloorage());
        System.out.println("getHpubdate======="+house.getHpubdate());
        System.out.println("getHcontact======="+house.getHcontact());
        System.out.println("getHdesc======="+house.getHdesc());
        System.out.println("getHflag======="+house.getHflag());
        if (house.getType()!=null)
            System.out.println("getType().getTid()======="+house.getType().getTid());
            System.out.println("getType().getTname()======="+house.getType().getTname());
        if (house.getMember()!=null)
            System.out.println("getMember().getMid()======="+house.getMember().getMid());
            System.out.println("getMember().getMname()======="+house.getMember().getMname());
        if (house.getStreet()!=null)
            System.out.println("getStreet().getSid()======="+house.getStreet().getSid());
            System.out.println("getStreet().getSname()======="+house.getStreet().getSname());
        if (house.getStreet().getDistrict()!=null)
            System.out.println("getDistrict()======"+house.getStreet().getDistrict());
            System.out.println("getDistrict().getDid()======"+house.getStreet().getDistrict().getDid());
            System.out.println("getDistrict().getDname()======"+house.getStreet().getDistrict().getDname());
        System.out.println("===================End=================");
    }

    //核查翻页的参数
    public void checkPagination(Pagination<House> pagination){
        System.out.println("getTotalRecords======"+pagination.getTotalRecords());
        System.out.println("getMaxRows======"+pagination.getMaxRows());
        System.out.println("getCurrPageNo======"+pagination.getCurrPageNo());
        System.out.println("getPrePageNo======"+pagination.getPrePageNo());
        System.out.println("getNextPageNo======"+pagination.getNextPageNo());
        System.out.println("getTotalPages======"+pagination.getTotalPages());
        System.out.println("getStartIndex======"+pagination.getStartIndex());
        System.out.println("getKeyWords======"+pagination.getKeyWords());
        if (pagination.getList()!=null)
        System.out.println("getList.size======"+pagination.getList().size());
        System.out.println();
    }

    public House getHouse() {
        return house;
    }

    public void setHouse(House house) {
        this.house = house;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public Map<String, Object> getObjectMap() {
        return objectMap;
    }

    public void setObjectMap(Map<String, Object> objectMap) {
        this.objectMap = objectMap;
    }

    public String getHtitle() {
        return htitle;
    }

    public void setHtitle(String htitle) {
        this.htitle = htitle;
    }

    public String getHprice_range() {
        return hprice_range;
    }

    public void setHprice_range(String hprice_range) {
        this.hprice_range = hprice_range;
    }

    public String getHfloorage_range() {
        return hfloorage_range;
    }

    public void setHfloorage_range(String hfloorage_range) {
        this.hfloorage_range = hfloorage_range;
    }

    public int getStreet_district_did() {
        return street_district_did;
    }

    public void setStreet_district_did(int street_district_did) {
        this.street_district_did = street_district_did;
    }

    public int getStreet_sid() {
        return street_sid;
    }

    public void setStreet_sid(int street_sid) {
        this.street_sid = street_sid;
    }

    public int getType_tid() {
        return type_tid;
    }

    public void setType_tid(int type_tid) {
        this.type_tid = type_tid;
    }

    public int getCurrPageNo() {
        return currPageNo;
    }

    public void setCurrPageNo(int currPageNo) {
        this.currPageNo = currPageNo;
    }

    public int getMaxRows() {
        return maxRows;
    }

    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    public int getMember_mid() {
        return member_mid;
    }

    public void setMember_mid(int member_mid) {
        this.member_mid = member_mid;
    }

    public String getOperater() {
        return operater;
    }

    public void setOperater(String operater) {
        this.operater = operater;
    }
}
